module.exports = {
    template: "#admin",
    data() {
        this.isAdmin = true;
        return window.rootcontroller;
    },
    mounted() {
        var self = this;
        $(this.$refs.navbardropdown).dropdown();
        rootcontroller.anuAdmin = this;
        self.globalAnuElement = {};
        self.globalAnuElement.aside = $(self.$refs.appaside);
        self.globalAnuElement.collpase = $(self.$refs.appcollpase);
        self.globalAnuElement.content = $(self.$refs.appcontent);
    },
    methods: {
        anuInitPageReady: function (pagecontext) {
            // 1
            var self = this;
            var userInfo = Lscache.get(rootcontroller.constatnts.USERINFO);
            rootcontroller.currentPagecontext = pagecontext;
            _.forEach(rootcontroller.adminroutes, function (v) {
                _.forEach(self.$route.matched, function (matchedroute) {
                    if (self.$route.matched && v.path === matchedroute.path) {
                        if (v.children) {
                            var res = v.children.map(function (a) {
                                a.to = v.path + "/" + a.path;
                                return a;
                            });

                            if (v.routefilter) {
                                // window.aui.interceptors[v.path] = {
                                //     origin: v,
                                //     admin: self,
                                //     method: v.interceptor
                                // };
                                res = res.filter(function (a, t) {
                                    return v.routefilter(a, t, userInfo);
                                });
                            }

                            rootcontroller.subroutenavs = res;
                        } else {
                            rootcontroller.subroutenavs = [];
                        }
                    }
                });
            });
            if (window.xxoo) {
                console.log('-------------------');
                console.log("from cookie");
                console.log('-------------------');
            }

            // if (rootcontroller.tokenManager.isExpired()) {
            //     self.$router.push({ path: '/signin' });
            // } else {
            //     self.getTokenFromCookie().then(function () {
            //         window.aui.Net.all("user-infos").then(function (response) {
            //             Lscache.set(rootcontroller.constatnts.USERINFO, response.data);
            //             self.userInfo = response.data;
            //             if (self.userInfo) {
            //                 self.app.globalUserName = self.userInfo.account;
            //             }
            //             self.runCurrentPage(pagecontext);
            //         });
            //     });
            // }
        },
        checkRouteActive(path, type){
            var self = this;
            var patharr = self.$route.fullPath.substring(1, self.$route.fullPath.length).split("/");
            var mainroute = "/" + patharr[0] + "/" + patharr[1];
            if (type === "main" && patharr.length > 1) {
                return mainroute === path;
            }
            if (type === "sub" && patharr.length === 2) {
                if (path === '') {
                    return true;
                }
            }
            if (type === "sub" && patharr.length > 2) {
                if (self.$route.matched[2].meta && self.$route.matched[2].meta.activesubroute) {
                    return self.$route.matched[2].meta.activesubroute(patharr);
                }
                return patharr[2] === path;
            }
            return false;
        },
        refreshToken() {
            var self = this;
            return new Promise(function (resolve, reject) {
                rootcontroller.services.requestService._put("tokens", 0, {
                }, {
                    token: rootcontroller.tokenManager.getToken()
                }) .then(function (response) {
                    let prop = {
                        token: response.data.token
                    };
                    rootcontroller.services.requestService.setProp(prop);
                    resolve(prop);
                }).catch(function (e) {
                    console.log('-------------------');
                    console.dir(e);
                    console.log('-------------------');
                    self.handleGlobalError(e);
                });
            });
        },
        handleGlobalError(e) {
            var self = this;
            console.dir(e);
            if (fuckIE9) {
                if (!e.response && !e.response.data || !e.response.data.code) {
                    e.response = {
                        data: {
                            code: 2002,
                            message: "令牌已过期"
                        }
                    };
                }
            }
            window.aui.toast({
                heading: '消息提示',
                text: e.response.data.message,
                icon: "error"
            });
            if (e && e.response && e.response.data && e.response.data .code == 2002) {
                self.gunchuqu();
                return false;
            }
            if (e && e.response && e.response.data && e.response.data.code == 2003) {
                self.loginout();
                return false;
            }
        },
        getTokenFromCookie() {
            return new Promise(function (resolve, reject) {
                let prop = {
                    token: rootcontroller.tokenManager.getToken()
                };
                rootcontroller.services.requestService.setProp(prop);
                resolve(prop);
            })
        },
        loginout() {
            var self = this;
            rootcontroller.tokenManager.clear();
            self.$router.push({ path: '/signin' });
            self.unBind();
            self.clearStore();
        },
        gunchuqu() {
            var self = this;
            rootcontroller.tokenManager.clear();
            self.$router.push({ path: '/signin' });
            self.unBind();
        },
        clearStore() {
            Lscache.remove(rootcontroller.constatnts.LASTROUTE);
            Lscache.remove(rootcontroller.constatnts.USERINFO);
        },
        unBind() {
            var self = this;
            self.userInfo = {};
            self.app.globalUserName = "";
        },
        toggleAppcollpase() {
            var self = this;
            self.globalAnuElement.collpase.toggleClass("show");
        },
        toggleOffScreen() {
            var self = this;
            if (self.globalAnuElement.aside.hasClass("off-screen")) {
                setTimeout(function() {                                
                    // self.globalAnuElement.content.removeClass("page-to-origin");
                    self.globalAnuElement.aside.removeClass("off-screen");
                }, 300);
            } else {                
                self.globalAnuElement.aside.addClass("off-screen");
            }
        },
        tooggleAside(e) {
            e.preventDefault();
            var self = this;
            self.app.settings.asideFolded = !self.app.settings.asideFolded;
        },
        onPageSelect(e) {
            e.preventDefault();
            var self = this;
            setTimeout(function() {
                self.toggleOffScreen();
            }, 0);
        },
        runCurrentPage(pagecontext) {
            if (pagecontext.service) {
                pagecontext.service();
            }
        }
    }
};